﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HA.Data;
using HAMath;


using System.Collections.ObjectModel;



namespace HA.ModFlow.Controls.Windows.DataOperation
{
    /// <summary>
    /// winInterpolateToModflow.xaml 的交互逻辑
    /// </summary>
    public partial class winInterpolateToModflow : Window
    {
        public winInterpolateToModflow(Scatter2D scatter, Project project)
        {
            InitializeComponent();
            this._scatter2D = scatter;
            this._project = project;
        }

        Scatter2D _scatter2D;

        public Scatter2D Scatter2D
        {
            get { return _scatter2D; }
            set { _scatter2D = value; }
        }
        Project _project;

        public Project Project
        {
            get { return _project; }
            set { _project = value; }
        }



        ObservableCollection<string> _scatterDataSet = new ObservableCollection<string>();

        ObservableCollection<ModflowLayer> _layers = new ObservableCollection<ModflowLayer>();

        ObservableCollection<ScatterToModflowLayer> _maps = new ObservableCollection<ScatterToModflowLayer>();

        public List<ScatterToModflowLayer> Maps
        {
            get {

                List<ScatterToModflowLayer> maps = new List<ScatterToModflowLayer>();

                maps.AddRange(this._maps);

                return maps;
            }
        }

        

       
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {


            foreach (string item in this.Scatter2D.ValueNames)
            {
                this._scatterDataSet.Add(item);
                
            }

            this.lstScatter.ItemsSource = this._scatterDataSet;


            this.InitModflowLayers();


            this.lstModflowData.ItemsSource = this._layers;

            this.dtGrid.ItemsSource = this._maps;



            if (this.lstScatter.Items.Count > 0)
            {
                this.lstScatter.SelectedIndex = 0;
            }
            if (this.lstModflowData.Items.Count > 0)
            {
                this.lstModflowData.SelectedIndex = 0;
            }
           

        }



        protected void InitModflowLayers()
        {
            this._layers.Clear();
            if (this.chkElevations.IsChecked.Value)
            {


                ModflowLayer layer = new ModflowLayer(ModflowLayerType.TOPELEVATION, "第1层顶部海拔", 1);

                this._layers.Add(layer);

                for (int i = 0; i < Project.MfData.GridData.LayersCount; i++)
                {

                    ///加入底层

                    layer = new ModflowLayer(ModflowLayerType.BOTELEVATION, "第" + (i + 1).ToString() + "层底部海拔", i);

                    this._layers.Add(layer);

                }

            }



            if (this.chkHeads.IsChecked.Value)
            {
                for (int i = 0; i < Project.MfData.GridData.LayersCount; i++)
                {

                    ModflowLayer layer = new ModflowLayer(ModflowLayerType.STARTINGHEAD, "第" + (i + 1).ToString() + "层初始水头", i);


                    this._layers.Add(layer);

                }
            }

            if (this.lstScatter.Items.Count > 0)
            {
                this.lstScatter.SelectedIndex = 0;
            }
            if (this.lstModflowData.Items.Count > 0)
            {
                this.lstModflowData.SelectedIndex = 0;
            }



        }




       




        private void chkElevations_Click(object sender, RoutedEventArgs e)
        {
            InitModflowLayers();
        }

        private void chkHeads_Click(object sender, RoutedEventArgs e)
        {
            InitModflowLayers();            
        }


        private void btnMap_Click(object sender, RoutedEventArgs e)
        {
            string scatter = this.lstScatter.SelectedItem as string;

            if (scatter == null)
            {
                return;

            }

            ModflowLayer layer = this.lstModflowData.SelectedItem as ModflowLayer;

            if (layer == null)
            {
                return;

            }

            ScatterToModflowLayer stl = new ScatterToModflowLayer(scatter, layer);

            this._maps.Add(stl);



            int mIndex = this.lstModflowData.SelectedIndex;
            this._layers.Remove(layer);


            if (mIndex >= 0 && this.lstModflowData.Items.Count > 0)
            {
                if (mIndex >= this.lstModflowData.Items.Count)
                {
                    mIndex = this.lstModflowData.Items.Count - 1;
                }
                this.lstModflowData.SelectedIndex = mIndex;
            }

        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
         
            this.Close();
        }

        private double[,] getArryResultData(int index)
        {
            List<HAMath.Point2D> pts = new List<HAMath.Point2D>();
            List<double> values = new List<double>();
            foreach (ScatterPoint scatter in this._scatter2D.DataSet)
            {
                HAMath.Point2D pt = new HAMath.Point2D();
                pt.X = scatter.X;
                pt.Y = scatter.Y;
                pts.Add(pt);
                double value = scatter.Values[index];
                values.Add(value);
            }

            //转换网格坐标　
            List<HAMath.Point2D> grid = new List<HAMath.Point2D>();

            for (int i = 0; i < this.Project.MfData.GridData.RowCoords.Length; i++)
            {
                for (int j = 0; j < this.Project.MfData.GridData.ColCoords.Length; j++)
                {
                    HAMath.Point2D pt = new HAMath.Point2D();
                    pt.X = this.Project.MfData.GridData.RowCoords[i] + this.Project.MfData.GridData.OrgX;
                    pt.Y = this.Project.MfData.GridData.ColCoords[j]+this.Project.MfData.GridData.OrgY ;
                    grid.Add(pt);
                }

            }

            List<double> result = HA.Data.IDWScatter2D.Interpolation(pts, values, grid,false,16);

            double[,] resu = new double[this.Project.MfData.GridData.RowsCount, this.Project.MfData.GridData.ColsCount];
            int p = 0;
            for (int i = 0; i < this.Project.MfData.GridData.RowsCount; i++)
            {
                for (int j = 0; j < this.Project.MfData.GridData.ColsCount; j++)
                {
                    resu[i, j] = result[p];
                    p++;
                }
            }
            return resu;
        }

        private void btnCancal_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnUnmap_Click(object sender, RoutedEventArgs e)
        {

            ScatterToModflowLayer item = this.dtGrid.SelectedItem as ScatterToModflowLayer;

            if (item!=null)
            {

               

                this._layers.Add(item.Layer);
                
            }
            int mIndex = this.dtGrid.SelectedIndex;
            this._maps.Remove(item);
            if (mIndex >= 0 && this.dtGrid.Items.Count > 0)
            {
                if (mIndex >= this.dtGrid.Items.Count)
                {
                    mIndex = this.dtGrid.Items.Count - 1;
                }
                this.dtGrid.SelectedIndex = mIndex;
            }


          
        }

        private void btnUnmapAll_Click(object sender, RoutedEventArgs e)
        {


            foreach (ScatterToModflowLayer item in this._maps)
            {

               

                this._layers.Add(item.Layer);

                
            }



            this._maps.Clear();
            if (this.lstModflowData.Items.Count > 0)
            {
                this.lstModflowData.SelectedIndex = 0;
            }
          
        }

        private void btnAutomap_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < this.lstScatter.Items.Count; i++)
            {
                string str = this.lstScatter.Items[i].ToString();
                if (str.Contains("bottom " +(i+1).ToString ()))
                {
                    for (int j = 0; j < this._layers.Count; j++)
                    {
                        if (this._layers[j].LayerType == ModflowLayerType.BOTELEVATION)
                        {
                            string scatter = str ;
                            if (scatter == null)
                            {
                                return;
                            }
                            ModflowLayer layer = this._layers[j];
                            if (layer == null)
                            {
                                return;
                            }

                            ScatterToModflowLayer stl = new ScatterToModflowLayer(scatter, layer);
                            this._maps.Add(stl);
                            this._layers.Remove(layer);
                            break;
                        }
                    }
                }
            }
        }

        private void btnInterOptions_Click(object sender, RoutedEventArgs e)
        {
            //win2DInterOptions win = new win2DInterOptions();
            //win.Owner = this;
            //win.ResizeMode = ResizeMode.NoResize;
            //win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            //win.ShowDialog();
        }


       

     
    }






  





  

   
}
